/*
 * =====================================================================================
 *
 *       Filename:  LinkedList.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  11/07/2012 12:59:12 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Kjeldahl (), 
 *   Organization:  
 *
 * =====================================================================================
 */
#include "LinkedList.h"
LinkedList * initLinkedList(){
	Linkeddata *lkd;
	lkd=createLinkeddata();
	LinkedList *L;
	L=malloc(sizeof(LinkedList));
	L->data=lkd;
	L->first=L->last=lkd;
	L->length=1;
	return L;

}
Linkeddata * createLinkeddata(){
	Linkeddata *lkd;
	lkd=malloc(sizeof(Linkeddata));
	lkd->pior=lkd->next=NULL;
	return lkd;
}

int insertafterLinkedList(LinkedList *L,Linkeddata *lkd,Linkeddata *a){
	if(lkd==L->last){
		a->next=NULL;
		lkd->next=a;
		a->pior=lkd;
		L->length++;
		L->last=a;
	}else{
	
		a->pior=lkd;
		a->next=lkd->next;
		lkd->next->pior=a;
		lkd->next=a;
		L->length++;
	}
	return 0;

}
int insertbeforeLinkedList(LinkedList *L,Linkeddata *lkd,Linkeddata *b){
	if(lkd==L->first){
		b->pior=NULL;
		lkd->pior=b;
		b->next=lkd;
		L->length++;
		L->first=b;
	}else{
	
		b->pior=lkd->pior;
		lkd->pior->next=b;
		lkd->pior=b;
		b->next=lkd;
		L->length++;
	}
	return 0;

}
Linkeddata * locateLinkedList(LinkedList *L,elmType value){
	int i=0;
	for(i=0;i<L->length;L->data=L->data->next,i++){
		printf("i:%d\n",i);
		if(L->data->info[0]==value)
			return L->data;
	}
	
	return NULL;
	


}
elmType * delLinkedList(LinkedList *L,Linkeddata *d){
	if(d==L->first){
		L->data=L->first=d->next;
		d->next->pior=NULL;
		L->length--;

	}else if(d==L->last){
		L->last=d->pior;
		d->pior->next=NULL;
		L->length--;
	}else{	
		d->next->pior=d->pior;
		d->pior->next=d->next;
		L->length--;
	}

	elmType * a=d->info;
	free(d);
	return a;
		
}
int listAll(LinkedList *L){
	int i=0;

	for(;i<L->length;L->data=L->data->next,i++)
		printf("%x\n",L->data->info[0]);
	L->data=L->first;
	return 0;
}

